home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / truedownloader / SetupTrueDownloader.exe / {app} / truedownloader.xpi / chrome / content / exUnregisterer.js < prev    next >
Encoding:
JavaScript  |  2004-06-16  |  15.5 KB  |  541 lines

  1. /*
  2. "exUnregisterer", the automatic unregisterer (Ver.0.4.2003041301) 
  3.  
  4. exapmle:
  5.  
  6. >var unreg = new exUnregisterer(
  7. >        'chrome://my_app/content/contents.rdf',
  8. >        'jar:%chromeFolder%my_app.jar!/locale/en-US/contents.rdf'
  9. >    );
  10. >
  11. >unreg.unregister(); // unregister all files
  12. >unreg.removePrefs('my_app'); // remove all prefs ('my_app.XXXX.XXXXX', 'my_app.YYYY.ZZZZ', and so on)
  13.  
  14.  
  15. This class has following properties and methods:
  16.  
  17. Chrome                   : the URI of the chrome directory in Mozilla
  18.                            installed.
  19. UChrome and UChrm        : the URI of the chrome directory in the current
  20.                            profile.
  21. unregister()             : executes unregisteration.
  22. removePrefs(aBranch)     : removes all preferences which include the handled
  23.                            name.
  24. readFrom(aFile)          : reads a text file and returns the content as
  25.                            a string.
  26. writeTo(aFile, aContent) : writes a text file with a string.
  27.  
  28.  
  29. When you create an instance of this class, you can use "%chromeFolder%"
  30. to point two "chrome" folders, in the directory Mozilla was installed in
  31. and in the profile directory.
  32.  
  33.  
  34.  
  35. */
  36.  
  37. /* ***** BEGIN LICENSE BLOCK *****
  38.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  39.  *
  40.  * The contents of this file are subject to the Mozilla Public License Version
  41.  * 1.1 (the "License"); you may not use this file except in compliance with
  42.  * the License. You may obtain a copy of the License at
  43.  * http://www.mozilla.org/MPL/
  44.  *
  45.  * Software distributed under the License is distributed on an "AS IS" basis,
  46.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  47.  * for the specific language governing rights and limitations under the
  48.  * License.
  49.  *
  50.  * The Original Code is the exUnregisterer.
  51.  *
  52.  * The Initial Developer of the Original Code is SHIMODA Hiroshi.
  53.  * Portions created by the Initial Developer are Copyright (C) 2002-2003
  54.  * the Initial Developer. All Rights Reserved.
  55.  *
  56.  * Contributor(s): SHIMODA Hiroshi <piro@p.club.ne.jp>
  57.  *
  58.  * Alternatively, the contents of this file may be used under the terms of
  59.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  60.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  61.  * in which case the provisions of the GPL or the LGPL are applicable instead
  62.  * of those above. If you wish to allow use of your version of this file only
  63.  * under the terms of either the GPL or the LGPL, and not to allow others to
  64.  * use your version of this file under the terms of the MPL, indicate your
  65.  * decision by deleting the provisions above and replace them with the notice
  66.  * and other provisions required by the GPL or the LGPL. If you do not delete
  67.  * the provisions above, a recipient may use your version of this file under
  68.  * the terms of any one of the MPL, the GPL or the LGPL.
  69.  *
  70.  * ***** END LICENSE BLOCK ***** */
  71.  
  72.  
  73.  
  74. function exUnregisterer()
  75. {
  76.     this.init(arguments);
  77.     delete this.mTarget.overlaysTemp;
  78. }
  79.  
  80. exUnregisterer.prototype =
  81. {
  82.     
  83.     // properties 
  84.     
  85.     mTarget : 
  86.     {
  87.         packages : [],
  88.         locales  : [],
  89.         skins    : [],
  90.         overlays : [],
  91.         overlaysTemp : []
  92.     },
  93.  
  94.     mEntriesURL : [], 
  95.  
  96.     get Chrome() 
  97.     {
  98.         if (!this._Chrome) {
  99.             this._Chrome = this.getURISpecFromKey('AChrom');
  100.             if (!this._Chrome.match(/\/$/)) this._Chrome += '/';
  101.         }
  102.         return this._Chrome;
  103.     },
  104.     _Chrome : null,
  105.  
  106.     get UChrome()
  107.     {
  108.         if (!this._UChrome) {
  109.             this._UChrome = this.getURISpecFromKey('UChrm');
  110.             if (!this._UChrome.match(/\/$/)) this._UChrome += '/';
  111.         }
  112.         return this._UChrome;
  113.     },
  114.     _UChrome : null,
  115.  
  116.     get UChrm()
  117.     {
  118.         return this.UChrome;
  119.     },
  120.  
  121.     get IOService() 
  122.     {
  123.         if (!this._IOService) {
  124.             this._IOService = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);
  125.         }
  126.         return this._IOService;
  127.     },
  128.     _IOService : null,
  129.  
  130.     get RDF() 
  131.     {
  132.         if (!this._RDF) {
  133.             this._RDF = Components.classes['@mozilla.org/rdf/rdf-service;1'].getService(Components.interfaces.nsIRDFService);
  134.         }
  135.         return this._RDF;
  136.     },
  137.     _RDF : null,
  138.  
  139.     get RDFC() 
  140.     {
  141.         if (!this._RDFC) {
  142.             this._RDFC = Components.classes['@mozilla.org/rdf/container;1'].createInstance(Components.interfaces.nsIRDFContainer);
  143.         }
  144.         return this._RDFC;
  145.     },
  146.     _RDFC : null,
  147.   
  148.     // Initialize 
  149.     init : function(aDsourcePaths)
  150.     {
  151.         var rootnode =
  152.             {
  153.                 packages : 'urn:mozilla:package:root',
  154.                 locales  : 'urn:mozilla:locale:root',
  155.                 skins    : 'urn:mozilla:skin:root',
  156.                 overlays : 'urn:mozilla:overlays'
  157.             },
  158.             dsource,
  159.             dsourcePaths = [],
  160.             i,
  161.             nodes,
  162.             node,
  163.             target;
  164.  
  165.         for (i = 0; i < aDsourcePaths.length; i++)
  166.         {
  167.             if (aDsourcePaths[i].match(/%chromeFolder%/i)) {
  168.                 dsourcePaths.push(aDsourcePaths[i].replace(/%chromeFolder%/gi, this.Chrome));
  169.                 dsourcePaths.push(aDsourcePaths[i].replace(/%chromeFolder%/gi, this.UChrome));
  170.             }
  171.             else
  172.                 dsourcePaths.push(aDsourcePaths[i]);
  173.         }
  174.  
  175.         for (var j = 0; j < dsourcePaths.length; j++)
  176.         {
  177.             try {
  178.                 if (this.RDF.GetDataSourceBlocking)
  179.                     dsource = this.RDF.GetDataSourceBlocking(dsourcePaths[j]).QueryInterface(Components.interfaces.nsIRDFDataSource);
  180.                 else
  181.                     dsource = this.RDF.GetDataSource(dsourcePaths[j]);
  182.             }
  183.             catch(e) {
  184.                 continue;
  185.             }
  186.  
  187.             for (i in rootnode)
  188.             {
  189.                 try {
  190.                     this.RDFC.Init(dsource, this.RDF.GetResource(rootnode[i]));
  191.                 }
  192.                 catch(e) {
  193.                     continue;
  194.                 }
  195.  
  196.                 nodes = this.RDFC.GetElements();
  197.                 while (nodes.hasMoreElements())
  198.                 {
  199.                     node = nodes.getNext();
  200.                     node = node.QueryInterface(Components.interfaces.nsIRDFResource);
  201.                     target = node.Value;
  202.                     switch(i)
  203.                     {
  204.                         case 'locales':
  205.                         case 'skins':
  206.                             target += ':packages';
  207.                             if (!this.mTarget[i][target])
  208.                                 this.mTarget[i][target] = [];
  209.                             break;
  210.  
  211.                         case 'overlays':
  212.                             if (!this.mTarget[i+'Temp'][target])
  213.                                 this.mTarget[i+'Temp'][target] = [];
  214.                             break;
  215.  
  216.                         default:
  217.                             this.mTarget[i][target] = true;
  218.                             break;
  219.                     }
  220.                 }
  221.             }
  222.  
  223.  
  224.             var targets =
  225.                 [
  226.                     this.mTarget.locales,
  227.                     this.mTarget.skins
  228.                 ];
  229.             for (var k in targets)
  230.             {
  231.                 for (i in targets[k])
  232.                 {
  233.                     try {
  234.                         this.RDFC.Init(dsource, this.RDF.GetResource(i));
  235.                     }
  236.                     catch(e) {
  237.                         continue;
  238.                     }
  239.                     nodes = this.RDFC.GetElements();
  240.                     while (nodes.hasMoreElements())
  241.                     {
  242.                         node = nodes.getNext();
  243.                         node = node.QueryInterface(Components.interfaces.nsIRDFResource);
  244.                         targets[k][i][node.Value] = true;
  245.                     }
  246.                 }
  247.             }
  248.  
  249.             // overlays
  250.             for (i in this.mTarget.overlaysTemp)
  251.             {
  252.                 try {
  253.                     this.RDFC.Init(dsource, this.RDF.GetResource(i));
  254.                 }
  255.                 catch(e) {
  256.                     continue;
  257.                 }
  258.                 nodes = this.RDFC.GetElements();
  259.                 while (nodes.hasMoreElements())
  260.                 {
  261.                     node = nodes.getNext();
  262.                     node = node.QueryInterface(Components.interfaces.nsIRDFLiteral);
  263.                     target = i.replace(/[^:]+:\/\/([^\/]+).+/, 'overlayinfo/$1/content/overlays.rdf');
  264.                     if (!this.mTarget.overlays[target])
  265.                         this.mTarget.overlays[target] = [];
  266.                     if (!this.mTarget.overlays[target][i])
  267.                         this.mTarget.overlays[target][i] = [];
  268.                     this.mTarget.overlays[target][i][node.Value] = true;
  269.                 }
  270.             }
  271.  
  272.         }
  273.  
  274.         return;
  275.     },
  276.     
  277.     // Get an URI from an internal keyword 
  278.     getURISpecFromKey : function(aKeyword)
  279.     {
  280.         const DIR = Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties);
  281.         var dir = DIR.get(aKeyword, Components.interfaces.nsIFile),
  282.             path;
  283.  
  284.         try {
  285.             path = this.IOService.newFileURI(dir).spec;
  286.         }
  287.         catch(e) { // [[interchangeability for Mozilla 1.1]]
  288.             path = this.IOService.getURLSpecFromFile(dir);
  289.         }
  290.  
  291.         return path;
  292.     },
  293.  
  294.     getURI : function(aKeyword) // old implementation
  295.     {
  296.         return this.getURISpecFromKey(aKeyword);
  297.     },
  298.  
  299.     // Convert an URI to a file path 
  300.     getFilePathFromURLSpec : function(aURL)
  301.     {
  302.         var url = Components.classes['@mozilla.org/network/standard-url;1'].createInstance(Components.interfaces.nsIURI);
  303.             url.spec = aURL;
  304.  
  305.         if (!url.schemeIs('file')) return '';
  306.  
  307.         var tempLocalFile;
  308.         try {
  309.             var fileHandler = this.IOService.getProtocolHandler('file').QueryInterface(Components.interfaces.nsIFileProtocolHandler);
  310.             tempLocalFile = fileHandler.getFileFromURLSpec(aURL);
  311.         }
  312.         catch(e) { // [[interchangeability for Mozilla 1.1]]
  313.             try {
  314.                 tempLocalFile = this.IOService.getFileFromURLSpec(aURL);
  315.             }
  316.             catch(ex) { // [[interchangeability for Mozilla 1.0.x]]
  317.                 tempLocalFile = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  318.                 this.IOService.initFileFromURLSpec(tempLocalFile, aURL);
  319.             }
  320.         }
  321.         return tempLocalFile.path;
  322.     },
  323.  
  324.     getFilePathFromURI : function(aURI) // old implementation
  325.     {
  326.         return this.getFilePathFromURLSpec(aURI);
  327.     },
  328.  
  329.     // Convert a file path to an URI 
  330.     getURLSpecFromFilePath : function(aPath)
  331.     {
  332.         var tempLocalFile = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  333.         tempLocalFile.initWithPath(aPath);
  334.  
  335.         try {
  336.             return this.IOService.newFileURI(tempLocalFile).spec;
  337.         }
  338.         catch(e) { // [[interchangeability for Mozilla 1.1]]
  339.             return this.IOService.getURLSpecFromFile(tempLocalFile);
  340.         }
  341.     },
  342.  
  343.     // does exist the file? 
  344.     exists : function(aFilePathOrURL)
  345.     {
  346.         if (aFilePathOrURL.match(/^file:/))
  347.             aFilePathOrURL = this.getFilePathFromURLSpec(aFilePathOrURL);
  348.  
  349.         var tempLocalFile = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  350.         tempLocalFile.initWithPath(aFilePathOrURL);
  351.         return tempLocalFile.exists();
  352.     },
  353.   
  354.     // Unregister information 
  355.     unregister : function()
  356.     {
  357.  
  358.         // packages unregisteration
  359.         for (i in this.mTarget.packages)
  360.         {
  361.             this.removeResources(this.Chrome+'chrome.rdf', 'urn:mozilla:package:root', this.mTarget.packages);
  362.             this.removeResources(this.UChrome+'chrome.rdf', 'urn:mozilla:package:root', this.mTarget.packages);
  363.             this.removeResources(this.Chrome+'all-packages.rdf', 'urn:mozilla:package:root', this.mTarget.packages);
  364.         }
  365.  
  366.         // locales unregistration
  367.         for (i in this.mTarget.locales)
  368.         {
  369.             this.removeResources(this.Chrome+'chrome.rdf', i, this.mTarget.locales[i]);
  370.             this.removeResources(this.UChrome+'chrome.rdf', i, this.mTarget.locales[i]);
  371.             this.removeResources(this.Chrome+'all-locales.rdf', i, this.mTarget.locales[i]);
  372.         }
  373.  
  374.         // skins unregistration
  375.         for (i in this.mTarget.skins)
  376.         {
  377.             this.removeResources(this.Chrome+'chrome.rdf', i, this.mTarget.skins[i]);
  378.             this.removeResources(this.UChrome+'chrome.rdf', i, this.mTarget.skins[i]);
  379.             this.removeResources(this.Chrome+'all-skins.rdf', i, this.mTarget.skins[i]);
  380.         }
  381.  
  382.         // overlays unregistration
  383.         for (i in this.mTarget.overlays)
  384.             for (j in this.mTarget.overlays[i])
  385.             {
  386.                 this.removeResources(this.Chrome+i, j, this.mTarget.overlays[i][j]);
  387.                 this.removeResources(this.UChrome+i, j, this.mTarget.overlays[i][j]);
  388.             }
  389.  
  390.  
  391.  
  392.         // remove entries from installed-chrome.txt
  393.         var installedChrome = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  394.         installedChrome.initWithPath(this.getFilePathFromURLSpec(this.Chrome+'installed-chrome.txt'));
  395.  
  396.         var entries = this.readFrom(installedChrome);
  397.         var regexp  = new RegExp();
  398.         for (i in this.mEntriesURL)
  399.             entries = entries.replace(regexp.compile('[^\\n\\r]+'+this.mEntriesURL[i]+'[\\n\\r]+', 'g'), '');
  400.         this.writeTo(installedChrome, entries);
  401.  
  402.  
  403.         return;
  404.     },
  405.     
  406.     // Remove info from RDF files 
  407.     removeResources : function(aDsourcePath, aRootURI, aTargets)
  408.     {
  409.         var dsource;
  410.         try {
  411.             var dsource = this.RDF.GetDataSource(aDsourcePath);
  412.                 dsource = dsource.QueryInterface(Components.interfaces.nsIRDFDataSource);
  413.         }
  414.         catch(e) {
  415.             return;
  416.         }
  417.  
  418.         try {
  419.             this.RDFC.Init(dsource, this.RDF.GetResource(aRootURI));
  420.         }
  421.         catch(e) {
  422. //            dump('ERROR: cannot remove resources in '+rootnode);
  423.             return;
  424.         }
  425.  
  426.         var nodes = this.RDFC.GetElements(),
  427.             node,
  428.             removenode,
  429.             removenodes = [],
  430.             removename,
  431.             removenames,
  432.             removevalue;
  433.  
  434.         while (nodes.hasMoreElements())
  435.         {
  436.             node = nodes.getNext();
  437.             try {
  438.                 node = node.QueryInterface(Components.interfaces.nsIRDFResource);
  439.             }
  440.             catch(e) {
  441.                 node = node.QueryInterface(Components.interfaces.nsIRDFLiteral);
  442.             }
  443.  
  444.             if (!node || (aTargets && !aTargets[node.Value])) continue;
  445.  
  446.             try {
  447.                 removenode = (aDsourcePath.match(/overlays\.rdf$/)) ? this.RDF.GetLiteral(node.Value) : this.RDF.GetResource(node.Value) ;
  448.  
  449.                 removenodes.push(removenode);
  450.  
  451.                 // If the file is "overlays.rdf", then this block is skipped.
  452.                 try {
  453.                     removenames = dsource.ArcLabelsOut(removenode);
  454.                     while (removenames.hasMoreElements())
  455.                     {
  456.                         removename = removenames.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  457.                         removevalue = dsource.GetTarget(removenode, removename, true);
  458.                         if (removename.Value.match(/#baseURL$/))
  459.                             this.mEntriesURL.push(removevalue.QueryInterface(Components.interfaces.nsIRDFLiteral).Value);
  460.  
  461.                         dsource.Unassert(removenode, removename, removevalue);
  462.                     }
  463.                 }
  464.                 catch(e) {
  465.                 }
  466.             }
  467.             catch(e) {
  468. //                dump('cannot remove '+node.Value+' from '+rooturi);
  469.             }
  470.         }
  471.  
  472.         for (var i in removenodes)
  473.             this.RDFC.RemoveElement(removenodes[i], true);
  474.  
  475.         // remove empty container from "overlays.rdf"
  476.         if (!this.RDFC.GetCount()) {
  477.             removenames = dsource.ArcLabelsOut(this.RDFC.Resource);
  478.             while (removenames.hasMoreElements())
  479.             {
  480.                 removename = removenames.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  481.                 removevalue = dsource.GetTarget(this.RDFC.Resource, removename, true);
  482.                 dsource.Unassert(this.RDFC.Resource, removename, removevalue);
  483.             }
  484.         }
  485.  
  486.         dsource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource).Flush();
  487.         return;
  488.     },
  489.   
  490.     // Remove all user preferences containing the argument "branch" in the top of the name. 
  491.     removePrefs : function(aBranch)
  492.     {
  493.         //const PREF = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefService).getBranch(branch+'.');
  494.         const PREF = Components.classes['@mozilla.org/preferences;1'].getService(Components.interfaces.nsIPrefBranch);
  495.  
  496.         try {
  497.             var prefs = PREF.getChildList(aBranch+'.', { value: 0 });
  498.             for (var i in prefs) PREF.clearUserPref(prefs[i]);
  499.         }
  500.         catch(e) {
  501. //            dump('ERROR: cannot clear user preferences.');
  502.         }
  503.  
  504.         return;
  505.     },
  506.  
  507.     // File I/O 
  508.     
  509.     readFrom : function(aFile) 
  510.     {
  511.         var stream = Components.classes['@mozilla.org/network/file-input-stream;1'].createInstance(Components.interfaces.nsIFileInputStream);
  512.         stream.init(aFile, 1, 0, false); // open as "read only"
  513.  
  514.         var scriptableStream = Components.classes['@mozilla.org/scriptableinputstream;1'].createInstance(Components.interfaces.nsIScriptableInputStream);
  515.         scriptableStream.init(stream);
  516.  
  517.         var fileSize = scriptableStream.available();
  518.         var fileContents = scriptableStream.read(fileSize);
  519.  
  520.         scriptableStream.close();
  521.         stream.close();
  522.  
  523.         return fileContents;
  524.     },
  525.  
  526.     writeTo : function(aFile, aContent) 
  527.     {
  528.         if (aFile.exists()) aFile.remove(true);
  529.         aFile.create(aFile.NORMAL_FILE_TYPE, 0666);
  530.  
  531.         var stream = Components.classes['@mozilla.org/network/file-output-stream;1'].createInstance(Components.interfaces.nsIFileOutputStream);
  532.         stream.init(aFile, 2, 0x200, false); // open as "write only"
  533.  
  534.         stream.write(aContent, aContent.length);
  535.  
  536.         stream.close();
  537.     }
  538.    
  539.  
  540.